﻿using System;
using System.Collections.Generic;
using NGinnBPM.Runtime.Events;
using NLog;
using System.Text;
using NGinnBPM.Lib.Data;
using NGinnBPM.Lib.Schema;
using System.Runtime.Serialization;

namespace NGinnBPM.Runtime.Tasks
{
    /// <summary>
    /// Raises an error. Raising error in a composite task cancels all currently active
    /// tasks and completes the composite with 'Failed' status.
    /// If the composite has an error outflow token will be sent along that flow.
    /// If there's no error outflow at current composite, operation repeats for its
    /// parent composite. If there's no error handler at all, process instance will
    /// complete with failed status.
    /// This task completes with 'failed' status.
    /// </summary>
    [Serializable]
    [DataContract]
    public class RaiseErrorTaskInstance : AtomicTaskInstance
    {
        private string _errorInfo;

        [DataMember]
        [TaskParameter(IsInput=true, Required=false, DynamicAllowed=true)]
        public string ErrorInfo
        {
            get { return _errorInfo; }
            set { _errorInfo = value; }
        }

        public override void Enable(Dictionary<string, object> inputData)
        {
            base.Enable(inputData);
            if (ErrorInfo == null) ErrorInfo="Error from task " + TaskId;
            DefaultHandleTaskFailure(ErrorInfo, true);
        }

        public override void Cancel()
        {
            throw new NotImplementedException();//can't be cancelled
        }

        public override void Complete(string finishedBy, Dictionary<string, object> updatedData)
        {
            throw new NotImplementedException();
        }
    }
}
